/**
 * 
 */
package org.wltea.analyzer.cfg;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.wltea.analyzer.dic.Dictionary;
import org.wltea.analyzer.help.Utils;
import org.wltea.analyzer.seg.DefaultSegmenter;
import org.wltea.analyzer.seg.ISegmenter;

/**
 * IK Analyzer v3.2 简单的配置管理类,单子模式
 * 
 * @author 林良益
 * 
 */
public class Configuration {

    /*
     * 分词器默认字典路径
     */
    public static final String DIC_MAIN = "/main.dict";
    public static final String DIC_STOP = "/stopword.dict";

    public static String KI_CONF_PATH = "ki_conf_path";
    //
    public static final String BASE_DICT = "base_dict";
    // 配置属性——扩展字典
    public static final String EXT_DICT = "ext_dict";
    // 配置属性——扩展停止词典
    public static final String EXT_STOP = "ext_stopwords";

    public static final String DEFAULT_EXT_DICT = "/org/wltea/analyzer/dic";

    private static Configuration CFG;

    private final Properties props = new Properties();

    private static Configuration getConfiguration() {
        if (CFG == null) {
            synchronized (Configuration.class) {
                if (CFG == null) {
                    CFG = new Configuration();
                }
            }
        }
        return CFG;
    }

    /*
     * 初始化配置文件
     */
    private Configuration() {
        String confPath = System.getProperty(KI_CONF_PATH);
        if (confPath == null) {
            String baseDict = System.getProperty(BASE_DICT);
            if (baseDict != null) {
                props.put(BASE_DICT, baseDict);
            }
            String extDict = System.getProperty(EXT_DICT);
            if (extDict != null) {
                props.put(EXT_DICT, extDict);
            }
            String extStop = System.getProperty(EXT_STOP);
            if (extStop != null) {
                props.put(EXT_STOP, extStop);
            }
            return;
        }
        InputStream input = Utils.loadFile(confPath);
        Configuration.class.getResourceAsStream(confPath);
        if (input == null) {
            throw new RuntimeException("cannot found ki conf file " + confPath);
        }
        try {
            if (confPath.endsWith(".xml")) {
                props.loadFromXML(input);
            } else {
                props.load(input);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取扩展字典配置路径
     * 
     * @return List<String> 相对类加载器的路径
     */
    public static List<String> getExtDictionarys() {
        return getDictionarys(EXT_DICT);
    }

    /**
     * 获取扩展停止词典配置路径
     * 
     * @return List<String> 相对类加载器的路径
     */
    public static List<String> getExtStopWordDictionarys() {
        return getDictionarys(EXT_STOP);
    }

    public static String getBaseDictionaryPath() {
        String extDictCfg = getConfiguration().props.getProperty(BASE_DICT);
        if (extDictCfg == null) {
            extDictCfg = DEFAULT_EXT_DICT;
        }
        return extDictCfg;
    }

    private static List<String> getDictionarys(String confName) {
        List<String> dictFiles = new ArrayList<String>(2);
        String extDictCfg = getConfiguration().props.getProperty(confName);
        if (extDictCfg != null) {
            // 使用;分割多个扩展字典配置
            String[] filePaths = extDictCfg.split(";");
            if (filePaths != null) {
                for (String filePath : filePaths) {
                    if (filePath != null && !"".equals(filePath.trim())) {
                        dictFiles.add(filePath.trim());
                    }
                }
            }
        }
        return dictFiles;
    }

    public static Properties getProperties() {
        return getConfiguration().props;
    }

    /**
     * 初始化子分词器实现 （目前暂时不考虑配置扩展）
     * 
     * @return List<ISegmenter>
     */
    public static List<ISegmenter> loadSegmenter() {
        // 初始化词典单例
        Dictionary.getInstance();
        List<ISegmenter> segmenters = new ArrayList<ISegmenter>(4);
        // 处理中文词的子分词器
        segmenters.add(new DefaultSegmenter());
        return segmenters;
    }
}
